package com.itrip.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrip.common.BigDecimalUtil;
import com.itrip.dao.ItripCommentMapper;
import com.itrip.dao.ItripHotelOrderMapper;
import com.itrip.dao.ItripImageMapper;
import com.itrip.entity.ItripComment;
import com.itrip.entity.ItripImage;
import com.itrip.service.ItripCommentService;
import com.itrip.service.ItripImageService;
import com.itrip.vo.comment.ItripCountCommentVO;
import com.itrip.vo.comment.ItripListCommentVO;
import com.itrip.vo.comment.ItripScoreCommentVO;
import com.itrip.vo.comment.ItripSearchCommentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class ItripCommentServiceImpl extends ServiceImpl<ItripCommentMapper, ItripComment> implements ItripCommentService{


 @Resource
 private ItripCommentMapper itripCommentMapper;

 @Override
 public ItripScoreCommentVO itripScoreCommentVo(Long hotelId) {
      return itripCommentMapper.itripScoreCommentVo(hotelId);
  }

 @Override
 public ItripCountCommentVO itripCountCommentVO(Long cityId) {
      return itripCommentMapper.itripCommentCount(cityId);
 }

 @Override
 public List<ItripListCommentVO> getlistComment(ItripSearchCommentVO itripSearchCommentVO) {
  return itripCommentMapper.getListComment(itripSearchCommentVO.getHotelId(),itripSearchCommentVO.getIsOk()
          ,itripSearchCommentVO.getIsHavingImg(),(itripSearchCommentVO.getPageNo()-1)*itripSearchCommentVO.getPageSize(),itripSearchCommentVO.getPageSize());
 }
 @Resource
 private ItripImageMapper itripImageMapper;
 @Resource
 private ItripHotelOrderMapper itripHotelOrderMapper;
 @Resource
 private ItripImageService itripImageService;

 @Override
 public boolean itriptxAddItripComment(ItripComment obj, List<ItripImage> itripImages) throws Exception {
   if(obj!=null){
    float score = 0;
    int sum = obj.getFacilitiesScore().intValue()+obj.getHygieneScore().intValue()+obj.getPositionScore().intValue()+obj.getServiceScore().intValue();
    score = BigDecimalUtil.OperationASMD(sum,4, BigDecimalUtil.BigDecimalOprations.divide,1, BigDecimal.ROUND_DOWN).floatValue();
    //对结果四舍五入
    obj.setScore(BigDecimal.valueOf(Math.round(score)));
    Long commentId = 0L;
    if(itripCommentMapper.insert(obj) > 0 ){
     commentId = obj.getId();
     log.debug("新增评论id：================ " + commentId);
     if(null != itripImages && itripImages.size() > 0 && commentId > 0){
      for (ItripImage itripImage:itripImages) {
       itripImage.setTargetId(commentId);
       itripImageMapper.insert(itripImage);
      }
     }
     //更新订单表-订单状态为4（已评论）
     itripHotelOrderMapper.updateHotelOrderStatus(obj.getOrderId(),obj.getCreatedBy());
     return true;
    }
   }
   return false;
  }

}
